﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Linq;
using COFE.Core;
using System.Net;

namespace COFE.Web
{
    /// <summary>
    /// Utils for manipulating Xml Streams.
    /// </summary>
    public static class XmlStreamUtils
    {
        #region ToXmlStream
        public static Stream ToXmlStream<T>(this T obj) 
            where T : ISupportXml
        {
            var ms = new MemoryStream();
            obj.ToXml().Save(new StreamWriter(ms));
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }


        #endregion

        #region FromXmlStream
        /// <summary>
        /// Construct an Entry object from Xml Stream.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static Entry EntryFromXmlStream(IUserProfile profile, Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));
            return FSIEntry.FromXml(profile, outerXml, false);
        }

        public static Entry EntryFromXmlStream(string host, Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));            
            return Entry.FromXElement(host, outerXml);
        }

        /// <summary>
        /// Construct an EntryType object from Xml Stream.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static EntryType EntryTypeFromXmlStream(IUserProfile profile, Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));
            return EntryType.FromXml(profile, outerXml, false);
        }
        
        /// <summary>
        /// Construct an EntryList object from Xml Stream.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static IEntryList EntryListFromXmlStream(IUserProfile profile, Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));
            return EntryList.FromXml(profile, outerXml);
        }

        public static IEntryList EntryListFromXmlStream(string host, Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));
            return EntryList.FromXml(host, outerXml);
        }

        /// <summary>
        /// Construct an Entry object from Xml Stream.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MetadataList MetadataFromXmlStream(Stream stream)
        {
            XElement outerXml = XElement.Load(new StreamReader(stream));
            return MetadataList.FromXml(outerXml);
        }
        #endregion
    }
}